package tests;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;

import reit.main.AssetsWithDammagedAssetsStorage;
import reit.main.ClerkDetails;
import reit.main.CustomerGroupDetails;
import reit.main.Management;
import reit.main.Parser;
import reit.main.RentalRequest;
import reit.main.RentalRequests;
import reit.main.RepairObjectInterface;
import reit.main.Warehouse;
import reit.xml.assets.Assets;

public class XMLParseBase {
	protected Logger fLogger;
	protected reit.main.AssetStorageInterface fAssets;
	protected RentalRequests fRentalRequests;
	protected reit.xml.customer.CustomersGroups fCustomersGroups;
	protected Management fManagement;
	protected Warehouse fWarehouse;
	protected List<ClerkDetails> fClerkDetails;
	protected List<CustomerGroupDetails> fCustomerGroupDetails;
	private LinkedList<RentalRequest> fRentalRequestsData;
	protected Hashtable<String, RepairObjectInterface> fReapirMaterials;
	protected Hashtable<String, RepairObjectInterface> fRepairTools;

	@Before
	public void setUp() throws Exception {
		fCustomersGroups = null;
		fRentalRequestsData = null;
		fAssets = null;
		this.fRentalRequests = new RentalRequests();
		this.fLogger = Logger.getLogger(XMLParseBase.class);
		fAssets = Parser.CreateAssets("src/xmls/Assets.xml");
		String path = "src/xmls/CustomersGroups.xml";
		try {
			JAXBContext jc = JAXBContext
					.newInstance(reit.xml.customer.CustomersGroups.class);
			JAXBContext jca = JAXBContext
					.newInstance(reit.xml.assets.Assets.class);
			Unmarshaller um = jc.createUnmarshaller();
			JAXBElement<reit.xml.customer.CustomersGroups> root = um.unmarshal(
					new StreamSource(path),
					reit.xml.customer.CustomersGroups.class); // creating a JAXBelement to store CustomerGroups no @XMLRootElement forces to do this from xml and putting value in assets
			this.fCustomersGroups = root.getValue();
			Unmarshaller uma = jca.createUnmarshaller();
			JAXBElement<Assets> roota = uma.unmarshal(new StreamSource(path),
					reit.xml.assets.Assets.class); // creating a JAXBelement to store Assets no @XMLRootElement forces to do this from xml and putting value in assets
			Assets assets = roota.getValue();

			for (int i = 0; fCustomersGroups.getCustomerGroups()
					.getCustomerGroupDetails().size() > i; i++) {
				this.fRentalRequestsData = Parser
						.createRentalRequestsContainer(fCustomersGroups
								.getCustomerGroups().getCustomerGroupDetails()
								.get(i));
				fRentalRequests.add(this.fRentalRequestsData);
			}
			fLogger.info("XML Parsing and population was successfull");
		} catch (Exception e) {
			fLogger.error("XML Parsing and population was not successfull");
			System.out.println(e.getMessage());
		}
		Hashtable<String, RepairObjectInterface> repairTools = Parser
				.CreateRepairTools("src/xmls/InitialData.xml");
		Hashtable<String, RepairObjectInterface> repairMaterials = Parser
				.CreateRepairMaterials("src/xmls/InitialData.xml");
		//			fWarehouse = new Warehouse(repairTools,repairMaterials);
		fCustomerGroupDetails = Parser.CreateCustomerGroupDetails(path);
		fClerkDetails = Parser.CreateClerks("src/xmls/InitialData.xml");
		fManagement = new Management(
				fClerkDetails,
				(AssetsWithDammagedAssetsStorage) fAssets,
				repairMaterials,
				repairTools,
				Parser.createAssetContentsRepairToolDetails("src/xmls/AssetContentsRepairDetails.xml"),
				Parser.createAssetContentsRepairMaterialDetails("src/xmls/AssetContentsRepairDetails.xml"),
				fCustomerGroupDetails, Parser
						.getNumberOfRentalRequests("src/xmls/InitialData.xml"),
				5);
		fReapirMaterials = repairTools;
		fReapirMaterials = repairMaterials;
	}

	@After
	public void tearDown() throws Exception {
	}

}
